﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace WellDone
{
    public sealed partial class OpenItemDialog : Page
    {
        public event EventHandler CloseRequested;
        public ServiceReferenceOpenRPOS.OpenItem openItem;
        public bool bUpdated = false;

        public OpenItemDialog()
        {
            this.InitializeComponent();
        }
        public void init()
        {
            var pane = InputPane.GetForCurrentView();
            pane.Showing += pane_Showing;
            pane.Hiding += pane_Hiding;
        }
        private void pane_Showing(InputPane sender, InputPaneVisibilityEventArgs e)
        {
            // Shift the dialog up...
            this.RootPanel.Margin = new Thickness(0, -1 * e.OccludedRect.Y, 0, 0);
        }
        private void pane_Hiding(InputPane sender, InputPaneVisibilityEventArgs e)
        {
            // Restore...
            this.RootPanel.Margin = new Thickness(0, 0, 0, 0);
        }
        private async void btnOK_Click(object sender, RoutedEventArgs e)
        {
            // Update information...
            openItem = new ServiceReferenceOpenRPOS.OpenItem();
            if (tbItemName.Text.Length > 0 && tbPrice.Text.Length>0)
            {
                openItem.col_Name = tbItemName.Text;
                try
                {
                    openItem.col_Price = Convert.ToDecimal(tbPrice.Text);
                }
                catch (Exception)
                {
                    openItem.col_Price = 0;
                }
                openItem.col_Description = tbDescription.Text;
                openItem.col_SendToKitchen = (cbSendToKitchen.IsChecked == true);
                // Set filters...
                openItem.bFilterDescription = true;
                openItem.bFilterName = true;
                openItem.bFilterPrice = true;
                openItem.bFilterSendToKitchen = true;
                // Add item to the database...
                openItem.col_ID = await Globals.client.AddOpenItemObjectAsync(Globals.token, openItem);
                bUpdated = true;
                // Close the dialog
                if (this.CloseRequested != null)
                {
                    this.CloseRequested(this, EventArgs.Empty);
                }
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            // Close the dialog
            if (this.CloseRequested != null)
            {
                this.CloseRequested(this, EventArgs.Empty);
            }
        }
    }
}
